package recallin.gui;

import recallin.activities.R;
import recallin.database.DatabaseUtility;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * A custom cursor adapter for displaying cursor information from the local SQLite database
 * @author philipmasek
 *
 */

public class NavigationAdapter extends CursorAdapter {

	public NavigationAdapter(Context context, Cursor c) {
		super(context, c);
	}
 
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView street = (TextView)view.findViewById(R.id.navigation_list_item_top);
		street.setText(cursor.getString(cursor.getColumnIndex("Street")));
		TextView city = (TextView)view.findViewById(R.id.navigation_list_item_bottom);
		city.setText(cursor.getString(cursor.getColumnIndex("City"))+", "+cursor.getString(cursor.getColumnIndex("State")));
	}
 
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.navigation_list_item, parent, false);
		bindView(v, context, cursor);
		return v;
	}
	
	@Override
	public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
		return getStreets(constraint, 50);
	}
	
	public Cursor getStreets(CharSequence constraint, int size){
		if (constraint == null  ||  constraint.length () == 0)  {
	        //  Return the full list of countries
	        return DatabaseUtility.getDatabase().rawQuery("SELECT * FROM Streets ORDER BY Street LIMIT "+size, null);
	    }  else  {
	        //  Return a list of countries who's name contains the passed in constraint
	        return DatabaseUtility.getDatabase().rawQuery("SELECT * FROM Streets WHERE Street LIKE '"+constraint.toString()+"%' ORDER BY Street LIMIT "+size, null);
	    }
	}
	
}